import cgi
import os

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp.util import login_required
from google.appengine.ext import db
from google.appengine.ext.webapp import template

class TestUsers(db.Model):
    first_name = db.StringProperty()
    last_name = db.StringProperty()
    email_address = db.EmailProperty(required=True)
    company_name = db.StringProperty()
    web_site = db.StringProperty()
    twitter_account = db.StringProperty()
    facebook_account = db.StringProperty()
    user_type = db.StringProperty()
    status = db.StringProperty()

class GetUsers(webapp.RequestHandler):
  @login_required
  def get(self):
    user = users.get_current_user()
    users_query = TestUsers.all()
    testusers = users_query.fetch(10)
    url = users.create_logout_url(self.request.uri)
    url_linktext = 'Logout'
    
    if users.is_current_user_admin():
      template_values = {
        'testusers': testusers,
        'url': url,
        'url_linktext': url_linktext,
        }
    else:
       template_values = {
        'url': url,
        'url_linktext': url_linktext,
        }
     
    path = os.path.join(os.path.dirname(__file__), 'users.html')
    self.response.out.write(template.render(path, template_values))

  
class SignupPage(webapp.RequestHandler):
  def get(self):
    template_values = {}

    path = os.path.join(os.path.dirname(__file__), 'signup.html')
    self.response.out.write(template.render(path, template_values))
    

class SignUp(webapp.RequestHandler):
  def post(self):
    testUser = TestUsers(last_name = self.request.get('last_name'),
                         first_name = self.request.get('first_name'),
                         email_address = self.request.get('email_address'),
                         company_name = self.request.get('company_name'),
                         web_site = self.request.get('web_site'),
                         twitter_account = self.request.get('twitter_account'),
                         facebook_account = self.request.get('facebook_account'),
                         user_type = self.request.get('user_type')
                         )
    testUser.put()
    self.redirect('/thankyou.html')

application = webapp.WSGIApplication(
                                     [('/signup', SignupPage),
                                      ('/newsignup', SignUp),
                                      ('/getusers', GetUsers)],
                                     debug=True)

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()
